package com.example.io;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 *.
 * 拷贝文件夹
 *
 *
 * */
public class Test02 {
    public static void main(String[] args) throws IOException {
        //数据源
        File src = new File("D:\\27162\\Desktop\\javaweb");
        File dest = new File("D:\\27162\\Desktop\\dest");

        copydir(src, dest);

    }

    private static void copydir(File src, File dest) throws IOException {

        dest.mkdirs();
        //获取src中每一个数据对象
        File[] files = src.listFiles();

        for (File file : files) {
            if (file.isFile()) {
                //是文件,直接拷贝
                FileInputStream inputStream = new FileInputStream(file);
                FileOutputStream outputStream = new FileOutputStream(new File(dest, file.getName()));
                int len;
                byte[] bytes = new byte[1024];//1kB
                while ((len = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, len);
                }
                //关闭输入流,输出流
                outputStream.close();
                inputStream.close();
            } else {
                //是文件夹,递归
                copydir(file, new File(dest, file.getName()));
            }
        }
    }
}
